<template>
    <div :class="{ 'container': true, 'sign-up-mode': isSignUp }">
        <div class="form-warp">
            <form class="sign-in-form">
                <h2 class="form-title">登录</h2>
                <a-input v-model:value="loginInfo.username" placeholder="用户名" style="width: 20%" />
                <a-input v-model:value="loginInfo.password" type="password" placeholder="密码" style="width: 20%" />
                <p :class="{ 'error-text': errEext }">{{ errEext }}</p>
                <div class="submit-btn" @click="toLogin">立即登录</div>
            </form>
            <form class="sign-up-form">
                <h2 class="form-title">注册</h2>
                <a-input v-model:value="registerInfo.username" placeholder="用户名" style="width: 20%" />
                <a-input v-model:value="registerInfo.password" type="password" placeholder="密码" style="width: 20%" />
                <a-select v-model:value="registerInfo.role" style="width: 200px" placeholder="请选择身份"
                    @change="handleSelectChange">
                    <a-select-option :value="1">普通用户</a-select-option>
                    <a-select-option :value="2">店铺管理员</a-select-option>
                    <a-select-option :value="3" disabled>后台管理员</a-select-option>
                </a-select>
                <p :class="{ 'error-text-1': errEext }">{{ errEext }}</p>
                <div class="submit-btn" @click="toRegister">提交申请</div>
            </form>
        </div>
        <div class="desc-warp">
            <div class="desc-warp-item sign-up-desc">
                <div class="content">
                    <button id="sign-up-btn" @click="toChangeBox">注册</button>
                    <div id="sign-up-text">还没有账号，点击进行注册</div>
                </div>
                <div class="system-title">Marketing System</div>
                <img src="../../assets/img/login.svg" alt="">
            </div>
            <div class="desc-warp-item sign-in-desc">
                <div class="content">
                    <button id="sign-in-btn" @click="toChangeBox">登录</button>
                    <div id="sign-in-text">已经有账号，点击进行登录</div>
                </div>
                <img src="../../assets/img/register.svg" alt="">
            </div>
        </div>
    </div>
</template>

<script>
import { defineComponent, reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { message } from "ant-design-vue";
import { login, register } from '../../service/api/user';

const useChangeAnimationEffect = () => {
    const isSignUp = ref(false);
    const toChangeBox = () => {
        isSignUp.value = !isSignUp.value;
    }
    return { isSignUp, toChangeBox }
}

// 登录
const useLoginEffect = (errEextRef) => {
    const router = useRouter();
    const loginInfo = reactive({
        username: '',
        password: '',
    })
    const toLogin = async () => {
        if (!loginInfo.username || !loginInfo.password) {
            errEextRef.value = '请输入完整';
        } else {
            errEextRef.value = '';
            const res = await login(loginInfo);
            if (res?.data?.id) {
                const { access_token, role } = res.data;
                localStorage.setItem("startTime", new Date().getTime().toString());
                localStorage.setItem("msToken", access_token);
                localStorage.setItem("role", role);
                localStorage.setItem("username", loginInfo.username);
                message.loading("登录成功，正在跳转……");
                setTimeout(() => {
                    message.destroy();
                    message.success("欢迎回来");
                    router.push({ name: "home" });
                }, 500);
            } else {
                message.error('账号不存在或密码错误');
            }
        }
    }
    return { loginInfo, toLogin }
}

// 注册
const useRegisterEffect = (errEextRef) => {
    const router = useRouter();
    const registerInfo = reactive({
        username: '',
        password: '',
    })
    const handleSelectChange = (selectValue) => {
        switch (selectValue) {
            case 1: // 普通用户
                message.info("该角色无需审核，注册后即可使用");
                break;
            case 2: // 店铺管理员
                message.info("店铺管理员需要进行审核");
                break;
        }
    }
    const toRegister = async () => {
        if (!registerInfo.username || !registerInfo.password || !registerInfo.role) {
            errEextRef.value = '请输入完整';
        } else if (registerInfo.username.length < 5) {
            errEextRef.value = '用户名至少为5位';
        } else if (registerInfo.password.length < 10) {
            errEextRef.value = '密码至少为10位';
        } else {
            errEextRef.value = '';
            const res = await register(registerInfo);
            if (res?.data?.id) {
                const { access_token, role } = res.data;
                localStorage.setItem("startTime", new Date().getTime().toString());
                localStorage.setItem("msToken", access_token);
                localStorage.setItem("role", role);
                localStorage.setItem("username", registerInfo.username);
                message.loading("登录成功，正在跳转……");
                setTimeout(() => {
                    message.destroy();
                    message.success("欢迎回来");
                    router.push({ name: "home" });
                }, 1000);
            } else {
                message.error(res?.msg || '注册失败');
            }
        }
    }
    return { registerInfo, handleSelectChange, toRegister }
}
export default defineComponent({
    setup() {
        const errEext = ref('');
        const { isSignUp, toChangeBox } = useChangeAnimationEffect();
        const { loginInfo, toLogin } = useLoginEffect(errEext)
        const { registerInfo, handleSelectChange, toRegister } = useRegisterEffect(errEext)
        return { errEext, isSignUp, loginInfo, registerInfo, toChangeBox, toLogin, handleSelectChange, toRegister };
    },
});
</script>

<style scoped>
* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    color: #333;
}

@font-face {
    font-family: Aerosol;
    src: url("../../assets/fonts/Aerosol.ttf");
}

.system-title {
    margin: -40px;
    font-size: 90px;
    color: #fff;
    font-family: 'Aerosol';
}

.error-text {
    color: red;
}

.error-text-1 {
    color: red;
}

#sign-up-btn,
#sign-in-btn {
    letter-spacing: 3px;
}

#sign-up-text,
#sign-in-text {
    color: #eee;
    padding-top: 10px;
}

.container {
    position: relative;
    min-height: 100vh;
    width: 100%;
    overflow: hidden;
}

.container::before {
    content: " ";
    position: absolute;
    width: 2000px;
    height: 2000px;
    border-radius: 50%;
    background-image: linear-gradient(-45deg, #6266f5 0%, #04befe 100%);
    transition: 1.8s ease-in-out;
    z-index: 6;
    top: -10%;
    right: 48%;
    transform: translateY(-50%);
}

.container.sign-up-mode::before {
    transform: translate(100%, -50%);
}

.form-warp {
    width: 50%;
    position: absolute;
    z-index: 5;
    left: 75%;
    top: 50%;
    z-index: 5;
    transform: translate(-50%, -50%);
    display: grid;
    grid-template-columns: 1fr;
    transition: 1s 0.7s ease-in-out;
}

.form-warp form {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 20px;
    /* 将两个 form 布局在 grid 同一位置 */
    grid-row: 1 / 2;
    grid-column: 1 / 2;
    transition: all 0.2s 0.7s;
    opacity: 1;
    z-index: 4;
}

.form-title {
    color: #6266f5;
}

.form-warp .sign-up-form {
    opacity: 0;
    z-index: 3;
}

.container.sign-up-mode .form-warp {
    left: 25%;
}

.container.sign-up-mode .sign-in-form {
    opacity: 0;
    z-index: 3;
}

.container.sign-up-mode .sign-up-form {
    opacity: 1;
    z-index: 4;
}

.ant-input,
.submit-btn {
    min-width: 300px;
    outline: none;
    padding: 12px 30px;
    line-height: 1;
    font-size: 16px;
    border-radius: 60px;
    color: #333;
    background-color: #6267f513;
    border: none;
}

.submit-btn {
    background-color: #6266f5;
    color: #FFF;
    text-align: center;
    min-width: 150px;
    font-size: initial;
    font-weight: bold;
    letter-spacing: 1.5px;
    cursor: pointer;
}

.desc-warp {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    display: grid;
    grid-template-columns: repeat(2, 1fr);
}

.desc-warp-item {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: space-around;
    text-align: center;
    text-align: center;
    padding: 3rem 17% 2rem 12%;
    z-index: 6;
}

/* 事件穿透 BEGIN */
.sign-in-desc {
    pointer-events: none;
}

.sign-up-mode .sign-in-desc {
    pointer-events: all;
}

.sign-up-mode .sign-up-desc {
    pointer-events: none;
}

/* 事件穿透 END */
.content {
    width: 100%;
    transition: transform 0.9s ease-in-out;
    transition-delay: .6s;
}

.sign-in-desc img,
.sign-in-desc .content {
    transform: translateX(800px);
}

.sign-up-mode .sign-in-desc img,
.sign-up-mode .sign-in-desc .content,
.sign-up-mode .sign-in-desc .system-title {
    transform: translateX(0);
}

.sign-up-mode .sign-up-desc img,
.sign-up-mode .sign-up-desc .content,
.sign-up-mode .sign-up-desc .system-title {
    transform: translateX(-800px);
}

button {
    outline: none;
    padding: 6px 8px;
    min-width: 100px;
    text-align: center;
    border-radius: 30px;
    border: 2px solid #FFF;
    background: none;
    color: #FFF;
    cursor: pointer;
    transition: all .3s ease;
}

button:active {
    background: rgba(255, 255, 255, .1);
}

img {
    width: 100%;
    display: block;
    transition: transform 0.9s ease-in-out;
    transition-delay: .5s;
}

.system-title {
    transition: transform 0.9s ease-in-out;
    transition-delay: .5s;
}

/* 响应式 */
@media screen and (max-width: 870px) {
    .container::before {
        width: 11000px;
        height: 1500px;
        transform: translateX(-50%);
        left: 30%;
        bottom: 68%;
        right: initial;
        top: initial;
        transition: 2s ease-in-out;
    }

    .container.sign-up-mode::before {
        transform: translate(-50%, 100%);
        bottom: 32%;
        right: initial;
    }

    .form-warp {
        width: 100%;
        top: 75%;
        left: 50%;
        transform: translate(-50%, -100%);
        transition: 1s 0.8s ease-in-out;
    }

    .container.sign-up-mode .form-warp {
        top: 25%;
        left: 50%;
        transform: translate(-50%, 0);
    }

    img {
        width: 200px;
        transition: transform 0.9s ease-in-out;
        transition-delay: 0.7s;
    }

    .desc-warp {
        grid-template-columns: 1fr;
        grid-template-rows: 1fr 2fr 1fr;
    }

    .desc-warp-item {
        flex-direction: row;
        justify-content: space-around;
        align-items: center;
        padding: 2.5rem 8%;
        grid-column: 1 / 2;
    }

    .sign-in-desc {
        grid-row: 3 / 4;
    }

    .sign-in-desc img,
    .sign-in-desc .content {
        transform: translateY(800px);
    }

    .sign-up-mode .sign-in-desc img,
    .sign-up-mode .sign-in-desc .content,
    .sign-up-mode .sign-in-desc .system-title {
        transform: translateY(0);
    }

    .sign-up-mode .sign-up-desc img,
    .sign-up-mode .sign-up-desc content,
    .sign-up-mode .sign-up-desc .system-title {
        transform: translateY(-800px);
    }

    .system-title {
        display: none;
    }
}

@media screen and (max-width: 570px) {
    .container::before {
        bottom: 72%;
        left: 50%;
    }

    img {
        display: none;
    }

    .system-title {
        display: none;
    }
}
</style>
